package com.ctrl.riveradministrator.mvp.view.activity.river_info;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.ctrl.riveradministrator.R;
import com.ctrl.riveradministrator.base.BaseActivity;
import com.ctrl.riveradministrator.base.BaseFragment;
import com.ctrl.riveradministrator.base.BaseListAdapter;
import com.ctrl.riveradministrator.http.download.DownloadListener;
import com.ctrl.riveradministrator.mvp.model.RiverInfo;
import com.ctrl.riveradministrator.mvp.presenter.RiverInfoPresenter;
import com.ctrl.riveradministrator.mvp.view.activity.river_doc.RiverDocActivity;
import com.ctrl.riveradministrator.mvp.view.activity.river_policy.RiverPolicyActivity;
import com.ctrl.riveradministrator.mvp.view.weight.BottomDialog;
import com.ctrl.riveradministrator.mvp.view.weight.RiverInfoDialog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class RiverInfoActivity extends BaseActivity<RiverInfoPresenter> implements RiverInfoView,
        BaseListAdapter.OnItemClickListener<RiverInfo>, BottomDialog.BottomDiaItemOnClickListener, DownloadListener {

    private Toolbar toolbar;
    private TextView titleTv;
    private ImageView leftMenu;
    private RiverInfoAdapter riverInfoAdapter;
    private RecyclerView bulletinRl;
    private SwipeRefreshLayout bulletinSfl;
    private LinearLayoutManager layoutManager;
    private BottomDialog bottomDialog;
    private RiverInfoDialog riverInfoDialog;
    private RiverInfo riverInfo;
    private ProgressDialog progressDialog;

    private List<RiverInfo> riverInfos = new ArrayList<>();
    private static int currentPage;

    @Override
    protected void initParams(Bundle parms) {

    }

    @Override
    protected int bindLayout() {
        return R.layout.activity_river_info;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void initView(View view) {
        toolbar = $(R.id.toolbar);
        titleTv = $(R.id.title_tv);
        leftMenu = $(R.id.left_iv);
        bulletinRl = $(R.id.policy_list);
        bulletinSfl = $(R.id.policy_srl);

        layoutManager = new LinearLayoutManager(this);
        bulletinRl.setLayoutManager(layoutManager);
        bulletinSfl.setColorSchemeColors(Color.RED, Color.BLUE);
        bulletinSfl.setEnabled(false);
        riverInfoAdapter = new RiverInfoAdapter(this);
        bulletinRl.setAdapter(riverInfoAdapter);

        bottomDialog = new BottomDialog(this);
        riverInfoDialog = new RiverInfoDialog(this);

        pDialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
        pDialog.setTitleText("加载中...");
        pDialog.setCancelable(false);
        progressDialog = new ProgressDialog(this);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMax(100);

        /*
         * 设置toolbar
         * */
        toolbar.setTitleTextColor(Color.WHITE);
        toolbar.setTitle("");
        titleTv.setText(getString(R.string.river_info));
        leftMenu.setBackground(getDrawable(R.drawable.ic_chevron_left_24dp));
    }


    @Override
    protected void doBusiness(Context mContext) {
        currentPage = 0;
        bulletinRl.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (bulletinRl.computeVerticalScrollExtent() + bulletinRl.computeVerticalScrollOffset()
                            >= bulletinRl.computeVerticalScrollRange()) {
                        // currentPage++;
                        currentPage = currentPage + 20;
                        mPresenter.getRiverPolicy(currentPage + "", "20");
                    }
                }
            }

            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        mPresenter.getRiverPolicy(currentPage + "", "20");
    }


    @Override
    protected void widgetClick(View view) {
        switch (view.getId()) {
            case R.id.left_iv:
                finish();
                break;
        }
    }

    @Override
    protected void setListener() {
        leftMenu.setOnClickListener(this);
        riverInfoAdapter.setOnItemClickListener(this);
        bottomDialog.setListener(this);
    }

    @Override
    public void handleMessage(Message message, int what) {

    }

    @Override
    public void showMessage(List<RiverInfo> riverPolicies) {
        this.riverInfos.addAll(riverPolicies);
        riverInfoAdapter.updateListView(this.riverInfos);
    }

    @Override
    public void onItemClick(RiverInfo riverInfo, int position) {
        this.riverInfo = riverInfo;
        bottomDialog.showBottomRiverInfoDialog(riverInfo.getRiverName());
    }

    @Override
    public void bottomDiaItem(int is) {
        switch (is) {
            case 0:
                Intent intent1 = new Intent(this, RiverDetailActivity.class);
                intent1.putExtra("river_info", riverInfo);
                startActivity(intent1);
                break;
            case 1:
                if(riverInfo.getZp() == null ||riverInfo.getZp().equals("")){
                    Toast.makeText(RiverInfoActivity.this, "不存在一河一策文件", Toast.LENGTH_LONG).show();
                    return;
                }
                progressDialog.setMessage(riverInfo.getZfm());
                ///file/5642ed3593b540d982ae5e0ea07c39de小白加速器电脑版(1).zip
                mPresenter.downloadFile(riverInfo.getZp(), this);
                progressDialog.show();
                //Intent intent = new Intent(this, RiverPolicyActivity.class);
                //intent.putExtra("river_info", riverInfo);
                //startActivity(intent);
                break;
            case 2:
                startActivity(new Intent(this, RiverDocActivity.class)
                        .putExtra("river_info", riverInfo));
                break;
            case 3:
                riverInfoDialog.showDialog(riverInfo.getId());
                break;
        }
        bottomDialog.cancel();
    }

    @Override
    protected RiverInfoPresenter createPresenter() {
        return new RiverInfoPresenter(this);
    }


    @Override
    public void showLoading() {
        super.showLoading();
        //pDialog.show();
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        //pDialog.cancel();
    }

    @Override
    public void onProgress(int progress) {
        RiverInfoActivity.this.runOnUiThread(() -> progressDialog.setProgress(progress));
    }

    @Override
    public void onCompleted(File file) {
        RiverInfoActivity.this.runOnUiThread(() -> {
            progressDialog.cancel();
            Toast.makeText(RiverInfoActivity.this, "请在"+file.getAbsolutePath()+"查看文件", Toast.LENGTH_LONG).show();
        });
    }

    @Override
    public void onError(String msg) {
        RiverInfoActivity.this.runOnUiThread(() -> {
            progressDialog.cancel();
            Toast.makeText(RiverInfoActivity.this, "系统错误，请重试...", Toast.LENGTH_LONG).show();
        });
    }
}
